﻿using System;
using System.Net.Http;
using System.Web.Http;
using AutoMapper;
using Common.Commands.AccountCommands;
using Dto.ApiRequests.AccountForms;
using NServiceBus;
using Sanelib.DataView;
using WebApp.ModelService;
using WebApp.Services;

namespace WebApp.ControllerApis
{
    public class AccountController : SmartApiController
    {
        public IBus Bus { get; set; }
        private readonly IAccountModelService _accountModelService;

        public AccountController(IUserSession userSession, IMappingEngine mappingEngine, IAccountModelService accountModelService)
            : base(userSession, mappingEngine)
        {
            _accountModelService = accountModelService;
        }

        public HttpResponseMessage Get([FromUri] SearchSpecification form)
        {
            var response = _accountModelService.GetPageBySpecification(form);
            return Content(response);
        }

        public HttpResponseMessage Post(AddAccountForm form)
        {
            var command = Mapper.Map<AddAccountForm, AddAccount>(form);
            return ExecuteCommand(command);
        }

        public HttpResponseMessage Put(UpdateAccountForm form)
        {
            var command = Mapper.Map<UpdateAccountForm, UpdateAccount>(form);
            return ExecuteCommand(command);
        }

        public HttpResponseMessage Delete(Guid id)
        {
            var command = new DeleteAccount(id);
            return ExecuteCommand(command);
        }
    }
}
